<template>
  <div class="mod-config">
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
      <el-form-item>
        <el-input v-model="dataForm.name" placeholder="姓名" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-input v-model="dataForm.idNumber" placeholder="身份证号" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-select v-model="dataForm.comparisonResult" clearable filterable placeholder="比对结果">
          <el-option v-for="item in dicts2" :key="item.value" :label="item.label" :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <span class="demonstration">时间</span>
        <el-date-picker
          v-model="dateValue"
          type="daterange"
          align="right"
          unlink-panels
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          value-format="yyyy-MM-dd">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList()">{{ $t('handle.search') }}</el-button>
        <!-- <el-button @click="idcardTest()">人证合一</el-button> -->
       <!--  <el-button v-if="isAuth('ic:witnesstestimony:save')" type="primary" @click="addOrUpdateHandle()">{{ $t('handle.add') }}</el-button> -->
        <el-button  v-if="isAuth('batch:delete')" type="danger" @click="deleteHandle()" :disabled="dataListSelections.length <= 0">{{ $t('handle.batchDelete') }}</el-button>
      </el-form-item>
    </el-form>
    <el-table
      :data="dataList"
      border
      v-loading="dataListLoading"
      @selection-change="selectionChangeHandle"
      style="width: 100%;">
      <el-table-column
        type="selection"
        header-align="center"
        align="center"
        width="50">
      </el-table-column>
      <el-table-column
        type="index"
       :label="$t('tabble.No')"
        width="50"
        :index="indexMethod">
      </el-table-column>
      <el-table-column
        prop="name"
        header-align="center"
        align="center"
        label="身份证姓名">
      </el-table-column>
      <el-table-column
        prop="idNumber"
        header-align="center"
        align="center"
        label="身份证号">
      </el-table-column>
      <el-table-column
        prop="comparisonType"
        header-align="center"
        align="center"
        label="认证类型">
        <template slot-scope="scope">
          <span>{{dictComparisonTypeFilter(scope.row.comparisonType)}}</span>
        </template>
      </el-table-column>
      <el-table-column
        prop="setThreshold"
        header-align="center"
        align="center"
        label="比对阀值">
      </el-table-column>
      <el-table-column
        prop="comparisonThreshold"
        header-align="center"
        align="center"
        label="比对分数">
      </el-table-column>
      <el-table-column
        prop="comparisonResult"
        header-align="center"
        align="center"
        label="比对结果">
        <template slot-scope="scope">
          <span>{{dictComparisonResultFilter(scope.row.comparisonResult)}}</span>
        </template>
      </el-table-column>
      <el-table-column
        prop="createDate"
        header-align="center"
        align="center"
        label="创建时间">
      </el-table-column>
      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        width="150"
        :label="$t('handle.handle')">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="addOrUpdateHandle(scope.row.id, true)">详细</el-button>
          <!-- <el-button type="text" size="small" @click="deleteHandle(scope.row.id)">{{ $t('handle.delete') }}</el-button> -->
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
    <!-- <id-card-verify v-if="true" ref="idCardVerify" @verifyResult="aaaaa"></id-card-verify> -->
  </div>
</template>

<script>
  import AddOrUpdate from './combinationWitnessTestimony-add-or-update'
  import idCardVerify from '@/components/IDCardVerify/IDCardVerify'
  export default {
    data () {
      return {
        dataForm: {
          name: '',
          idNumber: '',
        },
        dataList: [],
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false,
        dataListSelections: [],
        addOrUpdateVisible: false,
        dicts: [],
        dictType: {},
        dateValue: null,
        dicts2: [],
        dictType2: {},
      }
    },
    components: {
      AddOrUpdate,
      idCardVerify
    },
    activated () {
      this.getDict('idcard_comparison_type')
      this.getDict('idcard_comparison_result')
      this.getDataList()
    },
    computed: {
      dictComparisonTypeFilter() {
        return function(type) {
          /** do something */
          return this.dictType[type] ? this.dictType[type] : '未知';
        };
      },
      dictComparisonResultFilter() {
        return function(type) {
          return this.dictType2[type] ? this.dictType2[type] : '未知';
        }
      }
    },
    methods: {
      indexMethod(index) {
        return index + 1;
      },
      // 获取数据列表
      getDataList () {
        this.dataListLoading = true
        this.$http({
          url: '/dg-ic/ic/combination-witness-testimony/list',
          method: 'get',
          params: this.$http.adornParams({
            'page': this.pageIndex,
            'limit': this.pageSize,
            'name': this.dataForm.name,
            'idNumber':this.dataForm.idNumber,
            'comparisonResult': this.dataForm.comparisonResult,
            'startDate': this.dateValue == null ? "" : this.dateValue[0],
            'endDate': this.dateValue == null ? "" : this.dateValue[1]
          })
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.dataList = data.data.list
            this.totalPage = data.data.totalCount
          } else {
            this.dataList = []
            this.totalPage = 0
            this.$message.error(data.msg)
          }
          this.dataListLoading = false
        },
          error => {
            if (error.response.status === 403) {
              this.$message.error("你没有权限");
            } else {
              this.$message.error("系统错误，请联系管理员");
            }
          })
      },
      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageIndex = val
        this.getDataList()
      },
      // 多选
      selectionChangeHandle (val) {
        this.dataListSelections = val
      },
      // 新增 / 修改,是否可修改
      addOrUpdateHandle (id, formDisabled) {
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(id, formDisabled, this.dicts2)
        })
      },
      // 人证合一验证
      idcardTest(){
         this.$refs.idCardVerify.init ()
      },
      // 删除
      deleteHandle (id) {
        var ids = id ? [id] : this.dataListSelections.map(item => {
          return item.id
        })
        this.$confirm(`确定对[id=${ids.join(',')}]进行[${id ? '删除' : '批量删除'}]操作?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http({
            url: '/dg-ic/ic/combination-witness-testimony/delete',
            method: 'delete',
            data: this.$http.adornData(ids, false)
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.getDataList()
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        })
      },
      // 获取字典
      getDict(type){
        this.$http({
          url: "dg-ic/ic/dict/type/"+type,
          method: "get"
        }).then(({ data }) => {
          if (data && data.code === 0) {
            switch(type){
              case 'idcard_comparison_type':
                this.dicts = data.data;
                for (let i = 0; i < data.data.length; i++) {
                  this.dictType[data.data[i].value] = data.data[i].label;
                }
              break;
              case 'idcard_comparison_result':
                this.dicts2 = data.data;
                for (let i = 0; i < data.data.length; i++) {
                  this.dictType2[data.data[i].value] = data.data[i].label;
                }
              break;
            }
          } else if (data && data.code === 1) {
            this.$message.error(data.msg);
          }
        });
      }
    }
  }
</script>
